package com.example.makerspace.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.makerspace.dto.Result;
import com.example.makerspace.dto.robotDTO.Perception;
import com.example.makerspace.dto.robotDTO.RobotDTO;
import com.example.makerspace.dto.robotDTO.UserInfo;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.service.RobotService;
import com.google.gson.JsonObject;
import org.apache.http.Consts;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class RobotServiceImpl implements RobotService {
    private static final CloseableHttpClient httpclient = HttpClients.createDefault();
    @Override
    public Result invokeRobot(String spoken) {
        HashMap<String, String> map = new HashMap<>();
        map.put("appid","3320cb2cecdc45e791f041e17ceb9e63");
        map.put("userid","15970342814");
        map.put("spoken",spoken);
        String result = sendPost("https://api.sizhi.com/chat", map);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = (JSONObject) jsonObject.get("data");
        JSONObject info =(JSONObject) data.get("info");
        Object text = info.get("text");
        return  Result.success(text);
    }
    public static String sendPost(String url, Map<String,String> map) {
//        JsonObject formparams = new JsonObject();
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            formparams.add(entry.getKey(), entry.getValue();
//        }
        //json 格式
//        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        JsonObject jsonObject = new JsonObject();
        for(Map.Entry entry:map.entrySet()){
//            System.out.println(entry.getKey()+ "###########" + entry.getValue());
            jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
        }
//        System.out.println(jsonObject.toString());
        StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response= httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        org.apache.http.HttpEntity entity1 = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity1);
            System.out.println(result);
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
